
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.dto.DistributionUserBanDTO;
import com.jf.cloud.distribution.dto.DistributionUserDTO;
import com.jf.cloud.distribution.model.DistributionUser;
import com.jf.cloud.distribution.model.DistributionUserBan;
import com.jf.cloud.distribution.service.DistributionUserService;
import com.jf.cloud.distribution.service.DistributionUserWalletBillService;
import com.jf.cloud.distribution.vo.DistributionUserAchievementVO;
import com.jf.cloud.distribution.vo.DistributionUserBanVO;
import com.jf.cloud.distribution.vo.DistributionUserVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 分销员信息
 *
 * @author zz
 * @date 2021-08-09 14:14:07
 */
@RestController("platformDistributionUserController")
@RequestMapping("/p/distribution_user")
@Tag(name = "platform-分销员信息")
public class DistributionUserController {

    @Autowired
    private DistributionUserService distributionUserService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销员管理列表" , description = "分页获取分销员管理列表")
    public ServerResponseEntity<PageVO<DistributionUserVO>> page(@Valid PageDTO pageDTO ,@ParameterObject DistributionUserDTO distributionUserDTO) {
        PageVO<DistributionUserVO> distributionUserPage = distributionUserService.distributionUserPage(pageDTO, distributionUserDTO);
        setPhoneHide(distributionUserPage);
        return ServerResponseEntity.success(distributionUserPage);
    }

    @GetMapping("/achievement_page")
    @Operation(summary = "获取分销业绩统计列表" , description = "分页获取分销业绩统计")
    public ServerResponseEntity<PageVO<DistributionUserAchievementVO>> achievementPage(@Valid PageDTO pageDTO,@ParameterObject DistributionUserDTO distributionUserDTO, String userMobile) {
        PageVO<DistributionUserAchievementVO> page = distributionUserService.achievementPage(pageDTO, distributionUserDTO, userMobile);
        //计算累计收益
        for (DistributionUserAchievementVO distributionUserAchievementVO : page.getList()) {
            Long haveWithdrawalSum = distributionUserWalletBillService.getHaveWithdrawalSum(distributionUserAchievementVO.getWalletId());
            distributionUserAchievementVO.setAccumulateAmount(haveWithdrawalSum+distributionUserAchievementVO.getSettledAmount());
        }
        if (BooleanUtil.isFalse(permission)) {
            for (DistributionUserAchievementVO record : page.getList()) {
                if (StrUtil.isNotBlank(record.getNickName()) && PrincipalUtil.isMobile(record.getNickName())) {
                    record.setNickName(PhoneUtil.hideBetween(record.getNickName()).toString());
                }
                if (StrUtil.isNotBlank(record.getUserMobile())) {
                    record.setUserMobile(PhoneUtil.hideBetween(record.getUserMobile()).toString());
                }
            }
        }
        return ServerResponseEntity.success(page);
    }

    @Operation(summary = "修改分销员的状态")
    @PutMapping("/update_state")
    public ServerResponseEntity<Void> updateState(@RequestBody DistributionUserBanDTO distributionUserBanDTO){
        DistributionUserBan distributionUserBan = mapperFacade.map(distributionUserBanDTO, DistributionUserBan.class);
        distributionUserService.updateDistributionStateAndBan(distributionUserBan);
        // 清除缓存
        DistributionUser dbDistributionUser = distributionUserService.getByDistributionUserId(distributionUserBanDTO.getDistributionUserId());
        distributionUserService.removeCacheByUserId(dbDistributionUser.getUserId());
        return ServerResponseEntity.success();
    }

    @Operation(summary = "获取分销员最新的封禁信息")
    @GetMapping("/ban_info")
    public ServerResponseEntity<DistributionUserBanVO> getLatestBanInfo(@RequestParam(value = "distributionUserId") Long distributionUserId) {
        DistributionUserBanVO distributionUserBanVO = distributionUserService.getLatestBanInfoByDistributionUserId(distributionUserId);
        return ServerResponseEntity.success(distributionUserBanVO);
    }

    private void setPhoneHide(PageVO<DistributionUserVO> distributionUserPage) {
        if (BooleanUtil.isFalse(permission)){
            for (DistributionUserVO distributionUserVO : distributionUserPage.getList()) {
                if (StrUtil.isNotBlank(distributionUserVO.getInviteeMobile())) {
                    distributionUserVO.setInviteeMobile(PhoneUtil.hideBetween(distributionUserVO.getInviteeMobile()).toString());
                }
                if (StrUtil.isNotBlank(distributionUserVO.getUserMobile())) {
                    distributionUserVO.setUserMobile(PhoneUtil.hideBetween(distributionUserVO.getUserMobile()).toString());
                }
                if (StrUtil.isNotBlank(distributionUserVO.getInvitee()) && PrincipalUtil.isMobile(distributionUserVO.getInvitee())) {
                    distributionUserVO.setInvitee(PhoneUtil.hideBetween(distributionUserVO.getInvitee()).toString());
                }
                if (StrUtil.isNotBlank(distributionUserVO.getNickName()) && PrincipalUtil.isMobile(distributionUserVO.getNickName())) {
                    distributionUserVO.setNickName(PhoneUtil.hideBetween(distributionUserVO.getNickName()).toString());
                }
            }
        }
    }
}
